//
//  ExhibitionProjectController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/22.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "chaxunzhanlanViewController.h"

#import "ExhibitionProjectController.h"
#import "ExhibitionModel.h"
#import "ExhibitionCell.h"
#import "ExhibitionDetailController.h"


@interface ExhibitionProjectController ()

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,assign) NSInteger currentPage;

@end

@implementation ExhibitionProjectController

static NSString *const ID = @"ExhibitionProjectCell";

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = _string;
    self.tableView.backgroundColor = [UIColor whiteColor];
    _shenpi = -1;
    __weak typeof (self) weakSelf = self;
    
    [self.tableView addHeaderWithCallback:^{
        
        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
        
        [weakSelf MoreData];
        
    }];
     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"]  style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
        [self.navigationItem setRightBarButtonItem:rightButton];


}
- (void)rightButtonAction{

chaxunzhanlanViewController *vc  = [[chaxunzhanlanViewController alloc]init];
//vc.type = _type;
  if (self.type == ExhibtionCom) {
        _ty = 1;
    }else if(self.type == TimeCom){
        _ty = 2;
    }else{
    _ty = 0;
    
    
    }
vc.type = _ty;
vc.service = _service;
[self.navigationController pushViewController:vc animated:YES];


}

- (void)loadData{
    
    NSInteger ty = 0;
    
    if (self.type == ExhibtionCom) {
        ty = 1;
    }else if(self.type == TimeCom){
//   时代公司
        ty = 2;
    }
    else{
    
    ty = 0;
    }
    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': zlxmList,'service': %@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:%ld,branch:%ld,name:%@,endtime:%@,begintime:%@}'}",_service,userId,pwdId,userHumber,self.currentPage,_shenpi,ty,_name,_timeafterstring,_timebeforestring];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        [self.dataArray removeAllObjects];
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict2 = dict[@"attrs"];
            
            ExhibitionModel *model = [ExhibitionModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray addObject:model];
            
        }
        
        [self.tableView headerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}

- (void)MoreData{
    
    NSInteger ty = 0;
    
    if (self.type == ExhibtionCom) {
        ty = 1;
    }else if(self.type == TimeCom){
        //   时代公司
        ty = 2;
    }else if(self.type == QUANBU){
        //   时代公司
        ty = 0;
    }
    
    self.currentPage ++ ;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];

//    NSString *json = [NSString stringWithFormat:@"{'action': zlxmList,'service': %@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:-1,branch:%ld}'}",_service,userId,pwdId,userHumber,self.currentPage,ty];

    NSString *json = [NSString stringWithFormat:@"{'action': zlxmList,'service': %@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:-1,branch:%ld,name:%@,endtime:%@,begintime:%@}'}",_service,userId,pwdId,userHumber,self.currentPage,ty,_name,_timeafterstring,_timebeforestring];
    
    NSArray * array = @[@{@"arg0":json}];
    
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
        NSArray *arr = JSON[@"reason"];
        for (NSDictionary *dict in arr) {
            
            NSDictionary *dict2 = dict[@"attrs"];
            
            ExhibitionModel *model = [ExhibitionModel mj_objectWithKeyValues:dict2];
            
            [self.dataArray addObject:model];

            
        }
        
        [self.tableView footerEndRefreshing];
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    
}



#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    ExhibitionModel *model = self.dataArray[indexPath.row];
    
    ExhibitionCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [ExhibitionCell WithExhibitionCell];
        
    }
    
    cell.model = model;

    return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ExhibitionModel *model = self.dataArray[indexPath.row];
    
    ExhibitionDetailController *dVC = [[ExhibitionDetailController alloc] init];

    dVC.ID = model.ID;
    dVC.service = _service;
    [self.navigationController pushViewController:dVC animated:YES];
    
}


@end




















